<?php

/**
 * Configures an Mz_Configurable object.
 *
 * @author    Mon Zafra <monzee at gmail>
 * @copyright (c)2009 Mon Zafra
 * @category  Mz
 * @license   http://mz-project.googlecode.com/svn/trunk/LICENSE     MIT License
 * @version   SVN: $Id: Configure.php 3 2009-02-18 18:55:27Z monzee $
 */
class Mz_Configure
{
    /**
     * Sets the configuration of an Mz_Configurable object.
     * 
     * For each $key => $value pair of the array $config, the method 
     * setKey($value) of the configurable object is called if it is defined.
     * Otherwise, the catch-all setter setOption() is called.
     *
     * @param Mz_Configurable   $obj     Object to configure
     * @param Zend_Config|array $options Options, either an array or a Zend_Config
     */
    static public function setConfig(Mz_Configurable $obj, $options)
    {
        if ($options instanceof Zend_Config) {
            $options = $options->toArray();
        } else if (!is_array($options)) {
            $options = (array) $options;
        }

        foreach ($options as $option => $value) {
            $setter = 'set' . ucfirst($option);
            if (is_callable(array($obj, $setter))) {
                call_user_func(array($obj, $setter), $value);
            } else {
                $obj->setOption($option, $value);
            }
        }
    }

}
